<?php
class shopping extends Admin {

    public function index( $c, $a ) {
        $this->table = 'order';

        $where = array( G( 'k' ) => G( 'v' ) );


        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "wwId like '%{$key}%'" ) : '';
        }

        $this->pk = 'orderNo';
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];

        include $this->display( $c ); // 加载模板
    }

    public function edit( $c, $a ) {
        if ( !$id = intval( G( 'orderNo' ) ) ) {
            $this->error();
            exit;
        }

        $this->table = 'order'; // 数据库表名
        $this->pk = 'orderNo';
        $result = $this->find( array( 'orderNo' => $id ) ); // 查找数据
        include $this->display( $c ); // 加载模板
    }

    public function add( $c, $a ) {
        include $this->display( $c );
    }

    public function save() {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = ''; // 数据库表名
        if ( $id = intval( G( "id" ) ) ) {
            $msg = $this->update( array( 'id' => $id ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }
        exit( $msg );
    }
}
?>